package com.asf.sofom.repository;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.asf.sofom.domain.OUsers;

@Repository(value="usersDao")
public class JPAUsers implements UsersDao{

	private EntityManager em = null;
	
	@PersistenceContext
	public void setEntityManager(EntityManager em){
		this.em = em;
	}
	
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")	
	public List<OUsers> getUsers() {
		return em.createQuery("Select * from users").getResultList();
	}
	
	@Transactional(readOnly = false)
	public void guardaUser(OUsers user) {
		em.merge(user);
	}

	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")		
	public List<OUsers> buscaUser(OUsers user) {
		// TODO Auto-generated method stub
		TypedQuery<OUsers> tq = (TypedQuery<OUsers>) em.createNativeQuery("Select * from users where id_persona='" + user.getId_persona() + "'", OUsers.class);
		List<OUsers> lu = tq.getResultList();			
		return lu;
	}

}
